﻿// Please see documentation at https://docs.microsoft.com/aspnet/core/client-side/bundling-and-minification
// for details on configuring this project to bundle and minify static web assets.

// Write your JavaScript code.

function goPrepage(url) {
    location = getPrepage() || url || "/";
}

function getPrepage() {
    return new URLSearchParams(location.search).get('prepage');
}

function enable(selector) {
    $(selector).removeAttr('disabled');
}

function showInviteCode() {
    $.ajax({
        url: "/User/_InviteCode",
        success: function (data) {
            let $body = parseValidation(data);
            showModal("邀请码", $body);
        },
        error: JqueryAjaxError
    })
}

function showLogonModal() {
    $.ajax({
        url: "/LogOn/_Logon",
        success: function (data) {
            let $body = parseValidation(data);
            showModal("用户登录", $body);
        },
        error: JqueryAjaxError
    })
}

function parseValidation(data) {
    let $data = $(data);
    let $form = $data.is('form') ?
        $data :
        $(data).find('form');
    $.validator.unobtrusive.parse($form);
    return $data;
}

function showModal(title, body, footer) {
    $modal = $("[zyf-global-modal]");
    $modal.find(".modal-body").html(body);
    if (title) {
        $modal.find(".modal-header").show();
        $modal.find(".modal-title").html(title);
    }
    if (footer) {
        $modal.find(".modal-footer").html(footer).show();
    }
    else {
        $modal.find(".modal-footer").remove();
    }
    $modal.modal("show");
}

function formSubmit(event, settings) {
    event.preventDefault();
    var $form = $(event.currentTarget);
    if ($form.valid()) {
        $.ajax({
            url: settings.url || $form.attr('action'),
            data: $form.serialize(),
            method: 'post',
            success: function (data) {
                if (!data.succeed) {
                    parseAjaxError(data);
                    //注意区分failed和error的不同
                    if (settings.failed) {
                        settings.failed(data);
                    }
                    return false;
                }
                if (settings.success) {
                    settings.success(data);
                }
            },
            error: function (jqXHR, textStatus, errorThrown) {
                JqueryAjaxError(jqXHR, textStatus, errorThrown);
                if (settings.error) {
                    error(jqXHR, textStatus, errorThrown);
                }
            },
            complete: function () {
                if (settings.complete) {
                    complete();
                }
            }
        })
    } else {
        console.log('$form.valid()未通过');
    }
}

function parseAjaxError(data) {
    for (var e in data.errors) {
        $('[data-valmsg-for="' + e + '"]')
            .addClass('field-validation-error')
            .text(data.errors[e])
            .show();
    }

}

function JqueryAjaxError(jqXHR, textStatus, errorThrown) {
    console.log("ajax请求错误信息");
    console.log("jqXHR.readyState:" + jqXHR.readyState);
    if (jqXHR.readyState > 0) {
        console.log(jqXHR.responseText);
        console.log(textStatus);
        console.log(errorThrown);
    }
}

function hasError(data) {
    return Object.keys(data.errors).length > 0;
}

function getWarningIcon() {
    return '<span class="fa fa-frown-o text-warning"></span> ';
}

function showRemind(content) {
    showModal("<span class='fa fa-info-circle'></span> 友情提示", content);
}

function showRange(selector, value) {
    console.log(value);
    $(selector).text(value);
}

function isCurrent(userId) {
    return getCurrentUserId() == userId;
}

function getCurrentUserId() {
    return $.cookie("UserId");
}